<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/login.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
</head>

<body>
    <div class="container-login">
        <div class="container-pic">
            <img src="pic/computer.png" width="350px">
        </div>
        <div class="login-dialog">
            <h3>登陆</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" name="userName" id="userName" class="form-control">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" name="password" id="password" class="form-control">
            </div>
            <div class="row">
                <button type="button" class="btn btn-info btn-lg" onclick="login()">登录</button>
            </div>
        </div>
    </div>
     <script src="js/jquery.min.js"></script>
    <script>
        function login() {
            // 获取用户名和密码
            const userName = $("#userName").val().trim();
            const password = $("#password").val().trim();

            // 数据验证
            if (userName === "" || password === "") {
                alert("用户名和密码不能为空");
                return;
            }

            // 显示加载提示
            const loginButton = $("#loginButton");
            loginButton.prop("disabled", true).text("登录中...");

            $.ajax({
                type: "post",
                url: "/user/login",
                data: {
                    name: userName,
                    password: password
                },
                success: function (result) {
                    if (result) {
                        location.href = "book_list.html?currentPage=1";
                    } else {
                        alert("账号或密码错误");
                    }
                },
                error: function (xhr, status, error) {
                    // 处理请求失败的情况
                    alert("登录请求失败，请稍后重试。错误信息：" + error);
                },
                complete: function () {
                    // 隐藏加载提示
                    loginButton.prop("disabled", false).text("登录");
                }
            });
        }
    </script>
</body>
</html>